import BallGenerator from "./ballGenerator";

const {ccclass, property} = cc._decorator;

@ccclass
export default class GestureHandller extends cc.Component {

    @property(BallGenerator)
    private generator: BallGenerator = null;

    onEnable(){
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancle, this);
    }

    onDisable(){
        this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancle, this);
    }

    private onTouchStart(touch: cc.Event.EventTouch){
        this.generator.fireBall(touch.getLocation());
    }

    private onTouchMove(touch: cc.Event.EventTouch){
        this.generator.fireBall(touch.getLocation());
    }

    private onTouchEnd(touch: cc.Event.EventTouch){
        this.generator.stopFire();
        //       this.generator.fireBallToTarget(touch.getLocation());
    }

    private onTouchCancle(touch: cc.Event.EventTouch){
        this.generator.stopFire();
    }
}
